#include "stdafx.h"

LevelCircle::LevelCircle(const std::wstring & picname, double x, double y, int rotatespeed) :DynamicObject(x, y)
{
	image = ImageMgr::getInstance()->getImageFromName(L"img\\" + picname + L".png");
	this->rotatespeed = rotatespeed;

	width = image->GetWidth();
	height = image->GetHeight();
}

void LevelCircle::update(double dtime)
{
	angle += rotatespeed*dtime;

	while (angle >= 360)
	{
		angle -= 360;
	}
}

void LevelCircle::draw(Graphics * graphics)
{
	graphics->RotateTransform(angle, MatrixOrder::MatrixOrderAppend);
	graphics->TranslateTransform(x, y, MatrixOrder::MatrixOrderAppend);
	graphics->DrawImage(image, (REAL)(-width / 2.0), (REAL)(-height / 2.0));
	graphics->ResetTransform();
}

void LevelCircle::moveto(double x, double y)
{
	this->x = x;
	this->y = y;
}

EndPoint::EndPoint(const std::wstring & picname, double x, double y, int rotatespeed) :DynamicObject(x, y)
{
	image = ImageMgr::getInstance()->getImageFromName(L"img\\" + picname + L".png");
	this->rotatespeed = rotatespeed;

	width = image->GetWidth();
	height = image->GetHeight();
}

void EndPoint::update(double dtime)
{
	angle += rotatespeed*dtime;

	while (angle >= 360)
	{
		angle -= 360;
	}
}

void EndPoint::draw(Graphics * graphics)
{
	graphics->RotateTransform(angle, MatrixOrder::MatrixOrderAppend);
	graphics->TranslateTransform(x, y, MatrixOrder::MatrixOrderAppend);
	graphics->DrawImage(image, (REAL)(-width / 2.0), (REAL)(-height / 2.0));
	graphics->ResetTransform();
}

bool EndPoint::collide(const Rect & rc)
{
	int l = rc.GetLeft();
	int r = rc.GetRight();
	int t = rc.GetTop();
	int b = rc.GetBottom();

	double w = width / 2;
	double h = height / 2;

	return !(l < x - w && r < x - w || l > x + w && r > x + w || t < y - h && b < y - h || t > y + h && b > y + h);
}

int CollectionMgr::count = 0;

CollectionMgr::~CollectionMgr()
{
	for (EndPoint * c : collects)
	{
		delete c;
	}
}

CollectionMgr * CollectionMgr::getInstance()
{
	static CollectionMgr manager;

	return &manager;
}

void CollectionMgr::update(double dtime)
{
	std::vector<EndPoint *>::iterator co = collects.begin();

	while (co != collects.end())
	{
		(*co)->update(dtime);
		if ((*co)->collide(Game::getInstance()->getHero()->getRect()))
		{
			delete *co;
			co = collects.erase(co);
			count--;
		}
		else
		{
			co++;
		}
	}
}

void CollectionMgr::draw(Graphics * graphics)
{
	for (EndPoint * c : collects)
	{
		c->draw(graphics);
	}
}

void CollectionMgr::create(const std::wstring & picname, double x, double y, int rotatespeed)
{
	collects.push_back(new EndPoint(picname, x, y, rotatespeed));
	count++;
}

void CollectionMgr::clear()
{
	for (EndPoint * c : collects)
	{
		delete c;
	}

	collects.clear();
	count = 0;
}

int CollectionMgr::getCount()
{
	return count;
}